package com.todo.delicacy.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import com.todo.delicacy.infrastructure.entity.condition.RestaurantInfoCondition;
import com.todo.delicacy.infrastructure.entity.po.RestaurantInfoPO;
import com.todo.delicacy.infrastructure.mapper.RestaurantInfoMapper;
import com.todo.delicacy.infrastructure.repository.RestaurantInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (RestaurantInfo)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("restaurantInfoRepository")
public class RestaurantInfoRepositoryImpl implements RestaurantInfoRepository {
    @Resource
    private RestaurantInfoMapper restaurantInfoMapper;


    @Override
    public PageResult<RestaurantInfoPO> queryRestaurantInfoByCondition(RestaurantInfoCondition restaurantInfoCondition) {
        PageResult<RestaurantInfoPO> pageResult = new PageResult<>();
        if (restaurantInfoCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(restaurantInfoCondition.getPageIndex());
        pageResult.setPageSize(restaurantInfoCondition.getPageSize());
        long count = queryCountByCondition(restaurantInfoCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, restaurantInfoCondition.getPageSize()));
        pageResult.setTotal(count);
        List<RestaurantInfoPO> foodInfoPOList = restaurantInfoMapper.queryRestaurantInfoByCondition(restaurantInfoCondition);
        if (CollectionUtils.isEmpty(foodInfoPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(foodInfoPOList);
        pageResult.setCurrentIndex(foodInfoPOList.get(0).getId());
        pageResult.setHasNext(foodInfoPOList.size() >= restaurantInfoCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(RestaurantInfoCondition restaurantInfoCondition) {
        if (restaurantInfoCondition == null || restaurantInfoCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return restaurantInfoMapper.queryCountByCondition(restaurantInfoCondition);
    }

    @Override
    public RestaurantInfoPO queryRestaurantInfoPOById(Long id) {
        if (null == id || id < 0) {
            return null;
        }
        return restaurantInfoMapper.queryRestaurantInfoPOById(id);
    }

    @Override
    public Boolean insertRestaurantInfoPO(RestaurantInfoPO restaurantInfoPO) {
        if (restaurantInfoPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(restaurantInfoPO)) {
            return false;
        }
        return restaurantInfoMapper.insertRestaurantInfoPO(restaurantInfoPO) > 0;
    }

    @Override
    public Boolean updateRestaurantInfoPOById(RestaurantInfoPO restaurantInfoPO) {
        if (restaurantInfoPO == null || restaurantInfoPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(restaurantInfoPO)) {
            return false;
        }
        return restaurantInfoMapper.updateRestaurantInfoPOById(restaurantInfoPO) > 0;
    }


    @Override
    public Boolean removeRestaurantInfoPOById(Long id) {
        if (null == id || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        return restaurantInfoMapper.removeRestaurantInfoPOById(id) > 0;
    }

    @Override
    public RestaurantInfoPO queryRestaurantInfoByPoiId(Long storePoiId) {
        if (null == storePoiId || storePoiId < 0){
            return null;
        }
        return restaurantInfoMapper.queryRestaurantInfoByPoiId(storePoiId);
    }

    @Override
    public List<RestaurantInfoPO> queryRestaurantInfoByIdList(List<Long> restaurantIdList) {
        if (CollectionUtils.isEmpty(restaurantIdList)){
            return Collections.emptyList();
        }
        return restaurantInfoMapper.queryRestaurantInfoByIdList(restaurantIdList);
    }

    @Override
    public List<RestaurantInfoPO> queryRestaurantInfoByName(String restaurantName) {
        if (StringUtils.isBlank(restaurantName)){
            return Collections.emptyList();
        }
        return restaurantInfoMapper.queryRestaurantInfoByName(restaurantName);
    }

    /**
     * 基础字段校验
     *
     * @param restaurantInfoPO
     * @return
     */
    private boolean baseFieldCheck(RestaurantInfoPO restaurantInfoPO) {
        if (restaurantInfoPO == null) {
            return false;
        }
        if (restaurantInfoPO.getStorePoiId() == null || restaurantInfoPO.getName() == null || restaurantInfoPO.getProvince() == null || restaurantInfoPO.getCity() == null) {
            log.warn("RestaurantInfoRepository#baseFieldCheck 核心字段校验失败，操作失败 restaurantInfoPO：【{}】", JSONObject.toJSONString(restaurantInfoPO));
            return false;
        }
        if (restaurantInfoPO.getRestaurantType() == null) {
            log.warn("RestaurantInfoRepository#baseFieldCheck 基础字段校验失败，操作失败 restaurantInfoPO：【{}】", JSONObject.toJSONString(restaurantInfoPO));
            return false;
        }
        return true;
    }
}
